package com.aopeng.mapper;

import com.aopeng.entity.AopengExamination;
import com.aopeng.entity.AopengSchool;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * @author 谢辉
 * @description 考卷表持久层
 * @data 2020/7/10
 */
@Repository //代表持久层
public interface AopengExaminationMapper extends BaseMapper<AopengExamination> {
    //查询文章点击量
    @Select("select id,school_id,examination_name,click_number,create_time from aopeng_examination where school_id=#{id} order by click_number desc limit 12")
    List<AopengExamination> findClickNumber12(Long id);

    @Select("<script> SELECT id,school_id,examination_type,is_on_line,examination_name,examination_content,click_number,create_time,creator,update_time,updater,is_delete FROM aopeng_examination WHERE 1=1 <when test='type!=0'> and is_on_line = #{type} </when> ORDER BY create_time DESC limit 12 </script>")
//    @ResultMap("examinationMap")
        @Results(id = "examinationMap", value ={
            @Result(id = true,property = "id", column = "id"),
            @Result(property = "aopengSchool", column = "school_id", one = @One(select =
                "com.aopeng.mapper.AopengSchoolMapper.selectById"))
        })
    List<AopengExamination> findExaminationByDateSchool(int type);

    @Select("select a.id,a.school_id,a.examination_name,a.create_time,a.examination_type,a.click_number from aopeng_examination a inner join"
        + " ( SELECT id, school_id, examination_name,case when @school = school_id then @order_num:=@order_num+1 else @order_num:=1 end rownum, @school:=school_id FROM aopeng_examination,"
        + "(SELECT @order_num:= 0,@school := '') c order by school_id,create_time desc ) b"
        + " on a.id=b.id and a.school_id in (${ids}) where b.rownum <=12;")
    List<AopengExamination> findExaminationBySchool12(String ids);

}
